/* 
 * Copyright (c), nFractals Ltd. All Rights Reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.parade.distributed;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;

import org.parade.Constant;
import org.parade.Parade;
import org.parade.driver.Driver;
import org.parade.driver.ThreadExecutionService;

/**
 * Thread runs on arbitrary node on a cluster from given driver
 * 
 * @author		Lee moon soo
 *
 */
public class Thread implements Runnable, Object, Serializable{
	private static final long serialVersionUID = 7068071236685612160L;
	
	Id id;
	transient private Driver driver;
	transient private org.parade.distributed.AtomicBoolean started;
	transient private org.parade.distributed.AtomicBoolean completed;
	transient private Lock lock;
	transient private Condition cond;
	
	/**
	 * Create thread using driver provided by singleton Parade object
	 * 
	 * @param id		Cluster wide unique object id
	 */
	public Thread(Id id){
		initialize(Parade.getDriver(), id);
	}
	
	/**
	 * Create thread using given driver
	 * 
	 * @param driver	Distributed driver to use
	 * @param id		Cluster wide unique object id
	 */
	public Thread(Driver driver, Id id){
		initialize(driver, id);
	}
	
	public void initialize(Driver driver, Id id){
		this.driver = driver;
		this.id = id;
		started = new AtomicBoolean(driver, new Id(id.toString()+"-STARTED"));
		completed = new AtomicBoolean(driver, new Id(id.toString()+"-COMPLETED"));
		this.lock = new Lock(driver, new Id(id.toString()+Constant.ID_POSTFIX_LOCK));
		this.cond = new Condition(driver, lock);

	}

	@Override
	public Id getDistributedObjectId() {
		return id;
	}
	
	public void start(){
		ThreadExecutionService executionService = driver.getThreadExecutionService();
		executionService.add(this);
	}
	
	@Override
	public void run(){
		
	}

	public boolean isAlive(){
		if(started.get()==true && completed.get()==false) return true;
		else return false;
	}
	
	public void join() throws InterruptedException{
		lock.lock();
		try{
			while(completed.get() == false){
				cond.await();
			}
		} catch(InterruptedException e){
			throw e;
		} catch(Exception e){
			new RuntimeException(e);
		} finally{
			lock.unlock();
		}
	}
	
	public void join(long millis) throws InterruptedException{
		lock.lock();
		try{
			if(completed.get() == false){
				cond.await(millis, TimeUnit.MILLISECONDS);
			}
		} catch(InterruptedException e){
			throw e;
		} catch(Exception e){
			throw new RuntimeException(e);
		} finally{
			lock.unlock();
		}
	}
	
	public void join(long millis, int nanos) throws InterruptedException{
		join(millis);
	}
	

	public void onStarted() {
		started.set(true);
		
	}

	public void onCompleted() {
		lock.lock();
		try{
			completed.set(true);
			cond.signalAll();
		} catch(Exception e){
			throw new RuntimeException(e); 
		} finally{
			lock.unlock();
		}
	}

	
	
	
	
}
